#include "plugin.h"
#include "codeports.h"

void
PL_Load(PLUGIN *plugin, const char *library)
{
	if(!(plugin->handle = dlopen(library, RTLD_NOW)))
		fprintf(stderr, "Failed to open %s: %s\n", library, dlerror());
	plugin->DEMO_Init = dlsym(plugin->handle, "DEMO_Init");
	plugin->DEMO_Frame = dlsym(plugin->handle, "DEMO_Frame");
	plugin->DEMO_Final = dlsym(plugin->handle, "DEMO_Final");
}

void
PL_Unload(PLUGIN *plugin)
{
	dlclose(plugin->handle);
}

void
PL_Init(void)
{
	unsigned int i;
	for(i = 0; i < sizeof(plugins) / sizeof(PLUGIN); i++)
		PL_Load(&plugins[i], libraries[i]);
}

void
PL_Final(void)
{
	unsigned int i;
	for(i = 0; i < sizeof(plugins) / sizeof(PLUGIN); i++)
		PL_Unload(&plugins[i]);
}
